package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.hosp.mapper.HospitalSetMapper;
import com.atguigu.yygh.hosp.repository.HosipitalReposity;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HosipitalReposity hosipitalReposity;

    @Autowired
    private DictFeignClient dictFeignClient;


    @Override
    public void save(Map<String, Object> paramMap) {
        // 将map集合转换为对象
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        Hospital hospExit = hosipitalReposity.getHospitalByHoscode(hospital.getHoscode());
        if (hospExit != null) {
            hospital.setStatus(hospExit.getStatus());
            hospital.setCreateTime(hospExit.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hosipitalReposity.save(hospital);
        } else {
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hosipitalReposity.save(hospital);
        }
//        hosipitalReposity.findById(hospital.getHoscode())
//        hosipitalReposity.save(paramMap)
    }

    @Override
    public Hospital getByHoscode(String hoscode) {

        return hosipitalReposity.getHospitalByHoscode(hoscode);
    }

    @Override
    public Page selectHospPage(Integer page, Integer limit, HospitalQueryVo vo) {
        // 创建pageAble
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example 条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(vo,hospital);
        Example<Hospital> example = Example.of(hospital,matcher);
        Page<Hospital> all = hosipitalReposity.findAll(example, pageable);
        List<Hospital> content = all.getContent();
        content.forEach(item->{
            this.setHospitalType(item);
        });
        return all;
    }

    private Hospital setHospitalType(Hospital item) {
        String hostype = dictFeignClient.getName("Hostype", item.getHostype());
        String nameProvince = dictFeignClient.getName(item.getProvinceCode());
        String nameCity = dictFeignClient.getName(item.getCityCode());
        String nameDis = dictFeignClient.getName(item.getDistrictCode());
        item.getParam().put("hostypeString",hostype);
        item.getParam().put("fullAddress",nameProvince+nameCity+nameDis);
        return item;
    }
}
